package com.zzmcc.jms.controller;

import com.jfinal.aop.Before;
import com.jfinal.ext.render.excel.PoiRender;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.zzmcc.base.BaseController;
import com.zzmcc.common.AuthorizeInterceptor;
import com.zzmcc.jms.model.*;
import util.ChristStringUtil;


import com.jfinal.ext.kit.PoiKit.Pair;
import com.jfinal.ext.kit.PoiKit.SheetData;

import java.util.ArrayList;
import java.util.List;

import static com.zzmcc.jms.common.ModelConfigWms.TABLE_Tower;


public class TowerController extends BaseController {
    private static final Logger log =  Logger.getLogger(TowerController.class);
	@Before(AuthorizeInterceptor.class)
	public void index() {

        StringBuffer whee = new StringBuffer();
        List<Object> param = new ArrayList<Object>();
        String name = getPara("tower.name");
		String addr_code = getPara("tower.addr_code");
		String status = getPara("tower.status");
		String owner = getPara("tower.owner");
		String operator = getPara("tower.operator");
        String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		String home_county = getPara("tower.home_county");

		//高级搜索
		String addr_codes =getPara("addr_codes");
		if(ChristStringUtil.isNotEmpty(addr_codes)){
			String[] coderArr = ChristStringUtil.toArray(addr_codes, ",");
			int i=0;
			whee.append(" and addr_code in (\"");
			for (String code : coderArr) {
				i+=1;
				whee.append(code);
				if(i < coderArr.length)
					whee.append("\",\"");

			}
			whee.append("\")");
			System.out.print(whee);

		}

        if(ChristStringUtil.isNotEmpty(name)){
            whee.append(" and name like ?");
            param.add("%" + name + "%");
        }
		if(ChristStringUtil.isNotEmpty(addr_code)){
			whee.append(" and addr_code like ?");
			param.add("%" + addr_code + "%");
		}
		if(ChristStringUtil.isNotEmpty(operator)){
			whee.append(" and operator like ?");
			param.add("%" + operator + "%");
		}
		if(ChristStringUtil.isNotEmpty(status)){
			whee.append(" and status = ?");
			param.add(status);
		}
		if(ChristStringUtil.isNotEmpty(home_county)){
			whee.append(" and home_county = ?");
			param.add(home_county);
		}
		if(ChristStringUtil.isNotEmpty(owner)){
			whee.append(" and owner = ?");
			param.add(owner);
		}
        if(ChristStringUtil.isNotEmpty(beginDate)){
            whee.append(" and TO_DAYS(modifyDate) >= TO_DAYS(?)");
            param.add( beginDate );
        }
		if(ChristStringUtil.isNotEmpty(endDate)){
			whee.append(" and TO_DAYS(modifyDate) <= TO_DAYS(?)");
			param.add(endDate);
		}
		Page<Tower> pager = Tower.dao.paginate(
				getParaToInt("pageNum", 1),
				getParaToInt("numPerPage", 20),
                "select * ",
                " from " + TABLE_Tower +" where 1=1 "+ whee.toString()+"order by modifyDate desc",
                param.toArray());

		setAttr("pager",pager);
		setAttr("name",name);
		setAttr("addr_code",addr_code);
		setAttr("operator",operator);
		setAttr("owner",owner);
		setAttr("status",status);
		setAttr("beginDate",beginDate);
		setAttr("endDate",endDate);
		setAttr("home_county",home_county);
		render("../tower_list.html");
	}
	public void add() {
		render("../tower_input.html");
	}
	public void search() {
		render("../tower_search.html");
	}
	
	public void checkIsNotExist(){
		renderJson(checkIsNotExist(TABLE_Tower, "id", getPara("tower.id")));
	}
	
	public void checkNameIsNotExist(){
		renderJson(checkIsNotExist(TABLE_Tower, "name", getPara("tower.name")));
	}
	
	public void save() {
		Tower s = getModel(Tower.class, "tower");
		s.save();
		renderDWZSuccessJson("站址信息添加成功！");
	}
	
	public void edit() {
		setAttr("tower",Tower.dao.findById(getPara("id")));
		render("../tower_input.html");
	}
	
	public void update() {
		Tower s = getModel(Tower.class, "tower");
		s.update();
		renderDWZSuccessJson("修改成功！");
	}

	@Before(AuthorizeInterceptor.class)
	public void delete() {

		String[] ids = getParaValues("ids");
		for(String id:ids){
			Tower.dao.deleteById(id);
		}
		renderDWZSuccessJson("删除成功!");

	}

	public void export(){

		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		String name = getPara("tower.name");
		String addr_code = getPara("tower.addr_code");
		String status = getPara("tower.status");
		String owner = getPara("tower.owner");
		String operator = getPara("tower.operator");
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		String home_county = getPara("tower.home_county");

		//高级搜索
		String addr_codes =getPara("addr_codes");
		if(ChristStringUtil.isNotEmpty(addr_codes)){
			String[] coderArr = ChristStringUtil.toArray(addr_codes, ",");
			int i=0;
			whee.append(" and addr_code in (\"");
			for (String code : coderArr) {
				i+=1;
				whee.append(code);
				if(i < coderArr.length)
					whee.append("\",\"");

			}
			whee.append("\")");
			System.out.print(whee);

		}

		if(ChristStringUtil.isNotEmpty(name)){
			whee.append(" and name like ?");
			param.add("%" + name + "%");
		}
		if(ChristStringUtil.isNotEmpty(addr_code)){
			whee.append(" and addr_code like ?");
			param.add("%" + addr_code);
		}
		if(ChristStringUtil.isNotEmpty(operator)){
			whee.append(" and operator like ?");
			param.add("%" + operator + "%");
		}
		if(ChristStringUtil.isNotEmpty(status)){
			whee.append(" and status = ?");
			param.add(status);
		}
		if(ChristStringUtil.isNotEmpty(home_county)){
			whee.append(" and home_county = ?");
			param.add(home_county);
		}
		if(ChristStringUtil.isNotEmpty(owner)){
			whee.append(" and owner = ?");
			param.add(owner);
		}
		if(ChristStringUtil.isNotEmpty(beginDate)){
			whee.append(" and TO_DAYS(modifyDate) >= TO_DAYS(?)");
			param.add( beginDate );
		}
		if(ChristStringUtil.isNotEmpty(endDate)){
			whee.append(" and TO_DAYS(modifyDate) <= TO_DAYS(?)");
			param.add(endDate);
		}

		List<Record> list = Db.find("show full columns from jms_tower");
		List<Record> towerList =Db.find("select * from " + TABLE_Tower +
				" where 1=1 " + whee.toString() + "order by modifyDate desc",param.toArray());
		List<Pair> titles = new ArrayList<Pair>();
		for(Record r : list) {
			String value = r.get("Comment");
			String key = r.get("Field");
			titles.add(new Pair(key, value));
		}

		String sheetName ="tower";
		SheetData sheetData = new SheetData(titles,towerList,sheetName);
		PoiRender excel = PoiRender.me(sheetData);
		render(excel.fileName("towers.xls"));


	}



}
